﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Security.Cryptography;
using System.IO;
using System.Text;

using CE.Image;
using CE.User;
using CE.Company;
using CE.Global;

public partial class contactus_contactus : CE.Global.MyLanguageBasePage
{
    string _NavigateUrl = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        Master.Page.Title = Resources.Title.ContactTitle;

        LiteralControl newln = new LiteralControl(Environment.NewLine);
        HtmlMeta language = new HtmlMeta();
        language.Name = "language";
        language.Content = Resources.Title.PageLanguage;

        HtmlMeta keywords = new HtmlMeta();
        keywords.Name = "keywords";
        keywords.Content = Resources.Title.ContactKeyword;

        HtmlMeta description = new HtmlMeta();
        description.Name = "description";
        description.Content = Resources.Title.ContactDescription;

        Page.Header.Controls.AddAt(10, new LiteralControl(Environment.NewLine));
        Page.Header.Controls.AddAt(11, language);
        Page.Header.Controls.AddAt(12, new LiteralControl(Environment.NewLine));
        Page.Header.Controls.AddAt(13, keywords);
        Page.Header.Controls.AddAt(14, new LiteralControl(Environment.NewLine));
        Page.Header.Controls.AddAt(15, description);

        string _strCXLive = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.IsCXLive");
        if (_strCXLive == "0")
        {
            if (Session[CE.Global.General.Guest] == null)
            {
                Response.Redirect("~/Default.aspx");
            }
        }
        if (!Page.IsPostBack)
        {
            LogUserViewedHistory();

            CompanyInformation company = new CompanyInformation();
            CompanyInformationDB _companydb = new CompanyInformationDB();
            company = _companydb.GetCompanyInformation(4);
            if (company != null)
            {
                lblContactAddress.Text = company.Address1;
            } 
        }
    }

    private void LogUserViewedHistory()
    {
        UserViewHistoryDB _userviewdb = new UserViewHistoryDB();
        _userviewdb.LogViewedHistory("CTUS", Request, "Contact Us.");
    }

    protected void btnSendFeedBack_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            Int64 _result = 0;
            string strMessage = "";
            strMessage += "<br/>Sender : " + Server.HtmlEncode(CE.Global.General.ClearScriptTag(txtName.Text)) + "<br /><br />";
            strMessage += "<br/>Email : " + txtEmail.Text + "<br /><br />";
            strMessage += "What you feel?  : " + GetFeedback() + "<br /><br />";
            strMessage += "Feedback Type  : " + GetEmotion() + "<br /><br />";
            strMessage += "Message : <br/>" + Server.HtmlEncode(CE.Global.General.ClearScriptTag(txtFeedBack.Text)) + "<br />";

            UserFeedback _userfeedback = new UserFeedback();
            UserFeedbackDB _userfeedbackdb = new UserFeedbackDB();

            _userfeedback.FeedbackId = 0;
            _userfeedback.RefCEUserId = 0;
            if (Session[General.UserId] != null)
            {
                _userfeedback.RefCEUserId = Convert.ToInt64(Session[General.UserId].ToString());
            }
            _userfeedback.EmotionType = Convert.ToInt16(txtEmotionType.Text);
            _userfeedback.FeedbackType = GetFeedbackType();
            _userfeedback.Name = Server.HtmlEncode(CE.Global.General.ClearScriptTag(txtName.Text));
            _userfeedback.Email = txtEmail.Text.Trim();
            _userfeedback.Feedback = Server.HtmlEncode(CE.Global.General.ClearScriptTag(txtFeedBack.Text));
            _userfeedback.Status = true;
            _result = _userfeedbackdb.Insert(_userfeedback);

            if (CE.Mail.MyMailManager.SendFeedBack(txtEmail.Text, CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.FeedBackReceiver"), lblFeedBackTitle.Text, strMessage))
            {
                divThankYou.Visible = true;
                txtFeedBack.Enabled = true;
                btnSendFeedBack.Enabled = false;
            }
            else
            {

            }
        }
    }

    private Int16 GetFeedbackType()
    {
        Int16 _result = 0;
        if (txtFeedbackType.Text == "0")
        {
            _result = 1;
        }
        else
        {
            _result = Convert.ToInt16(txtFeedbackType.Text);
        }
        return _result;
    }

    private string GetFeedback()
    {
        string _result = "";
        if (txtFeedbackType.Text == "1")
        {
            _result = "Suggestion";
        }
        else if (txtFeedbackType.Text == "2")
        {
            _result = "Compliment";
        }
        else if (txtFeedbackType.Text == "3")
        {
            _result = "Bug Report";
        }
        return _result;
    }

    private string GetEmotion()
    {
        string _result = "";
        if (txtEmotionType.Text == "1")
        {
            _result = "Very Useful";
        }
        else if (txtEmotionType.Text == "2")
        {
            _result = "Good";
        }
        else if (txtEmotionType.Text == "3")
        {
            _result = "OK";
        }
        else if (txtEmotionType.Text == "4")
        {
            _result = "Sad";
        }
        else if (txtEmotionType.Text == "5")
        {
            _result = "Angry";
        }
        return _result;
    }

}
